package com.llyb.tingshu.user.api;

import com.llyb.tingshu.common.login.TsLogin;
import com.llyb.tingshu.common.result.Result;
import com.llyb.tingshu.common.util.AuthContextHolder;
import com.llyb.tingshu.user.service.UserInfoService;
import com.llyb.tingshu.vo.user.UserInfoVo;
import com.llyb.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;


	/**
	 * 处理用户购买记录
	 * @param userPaidRecordVo
	 * @return
	 */
	@Operation(summary = "处理用户购买记录")
	@PostMapping("/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		// 调用服务层方法.
		userInfoService.savePaidRecord(userPaidRecordVo);
		// 默认返回
		return Result.ok();
	}


	/**
	 * 获取专辑已支付的声音Id集合列表
	 * @param albumId
	 * @return
	 */
	@TsLogin
	@GetMapping("findUserPaidTrackList/{albumId}")
	Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){

		Long userId = AuthContextHolder.getUserId();
		List<Long> list = userInfoService.findUserPaidTrackList(albumId,userId);
		return Result.ok(list);
	}

	@Operation(summary = "获取用户信息")
	@GetMapping("/getUserInfo/{userId}")
	public Result getUserInfo(@PathVariable("userId") Long userId) {

		UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
		return Result.ok(userInfoVo);
	}

	/**
	 * 判断用户是否购买了那些专辑
	 * @param albumId
	 * @param trackIdList
	 * @return
	 */
	@TsLogin(required = false)
	@Operation(summary = "判断用户是否购买了那些专辑")
	@RequestMapping("/userIsPaidTrack/{albumId}")
	public Result userIsPaidTrack(@PathVariable("albumId") Long albumId, @RequestBody List<Long> trackIdList) {

		Long userId = AuthContextHolder.getUserId();
		Map<Long,Integer> map =  userInfoService.userIsPaidTrack(albumId, trackIdList,userId);

		return Result.ok(map);
	}


	/**
	 * 判断用户是否购买过专辑
	 * @param albumId
	 * @return
	 */
	@TsLogin
	@Operation(summary = "判断用户是否购买过专辑")
	@GetMapping("isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
		// 获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		// 调用服务层方法
		Boolean flag = userInfoService.isPaidAlbum(userId, albumId);
		return Result.ok(flag);
	}
}

